getOnlyOneFromPhone = function () {
    var text = device.getModel() + "-" +
        device.getBrand() + "-" +
        device.getAndroidId() + "-" +
        device.getIMEI() + "-" +
        device.getScreenHeight() + "-" +
        device.getScreenWidth();
    return utils.dataMd5(text);
};

getOnlyOneFromPhone3 = function (time, new_validFile_ui_other) {
    var text = device.getModel() + "-" +
        device.getBrand() + "-" +
        device.getScreenHeight() + "-" +
        device.getScreenWidth();
    var two = utils.dataMd5(text).substring(0, 15).toUpperCase();
    if (file.exists(new_validFile_ui_other)) {
        var txt = file.readLine(new_validFile_ui_other, 1);
        if (txt.split("WL")[1] === two) {
            var url = "https://gitee.com/baishui0212/redbook/raw/master/" + time + "/" + txt;
            var my = http.httpGetDefault(url, 5 * 1000);
            if (my.toLowerCase().indexOf("ok") === -1) {
                var my11 = http.httpGetDefault("https://gitee.com/baishui0212/redbook/raw/master/" + txt, 5 * 1000);
                if (my11.toLowerCase().indexOf("ok") === -1) {
                    loge("远程注册设备失败，请联系管理员");
                    exit();
                } else {
                    // 最终的方案
                    valid_expire(my);
                }
            } else {
                // 最终的方案
                valid_expire(my);
            }
        } else {
            logd("bad gay! out");
            exit();
        }
    } else {
        logd("bad gay! out!");
        loge("远程注册设备失败，请联系管理员");
        exit();
    }

};

valid_expire = function (str) {
    var array = str.split("#");
    if(array.length === 2){
        var endTime = array[1];
        if(endTime === ""){
            loge("你没有续费... 拜拜啦~");
            exit();
        }
        var nowTime = timeFormat("yyyyMMdd");
        if (nowTime > endTime) {
            loge("{} 到期啦，你该续费啦...", endTime);
            exit();
        }
    }
};


valid = function () {
    var new_validFile = "/sdcard/newActive/other.txt";
    var new_Time_ = "/sdcard/newActive/time.txt";
    var time;
    if (file.exists(new_Time_)) {
        time = file.readLine(new_Time_, 1).substring(0, 6);
    }
    var id = device.tcDeviceId();
    loge("远程注册设备...");
    var url = "https://gitee.com/baishui0212/redbook/raw/master/" + time + "/" + id;
    var x;
    try {
        x = http.httpGetDefault(url, 5 * 1000);
    } catch (e) {
        x = "";
        loge("request error {}", e);
    }
    var index = x.toLowerCase().indexOf("ok");
    logd("valid flag {}", index);
    if (index === -1) {
        try {
            x = http.httpGetDefault("https://gitee.com/baishui0212/redbook/raw/master/" + id, 5 * 1000);
        } catch (e) {
            x = "";
            loge("request error {}", e);
        }
        if (x.toLowerCase().indexOf("ok") === -1) {
            var myOnly = getOnlyOneFromPhone();
            x = http.httpGetDefault("https://gitee.com/baishui0212/redbook/raw/master/other/" + myOnly, 5 * 1000);
            if (x.toLowerCase().indexOf("ok") === -1) {
                getOnlyOneFromPhone3(time, new_validFile);
            } else {
                // other 方案
                valid_expire(x);
            }
        } else {
            // 外层的 tcDeviceId
            valid_expire(x);
        }
    } else {
        // tcDeviceId
        valid_expire(x);
    }
};

valid();